package com.rongc.client.freight.base.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Bruce on 2016/12/2.
 */
public abstract class RecyclerBaseAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected Context mContext;
    protected List<T> mDatas;
    public OnItemClickListener<T> mOnItemClickListener;

    public interface OnItemClickListener<T> {
        void onItemClick(View view, int position, T data);
    }

    public RecyclerBaseAdapter(){
        this(null);
    }

    public RecyclerBaseAdapter(Context mContext){
        this(mContext,null);
    }

    public RecyclerBaseAdapter(Context mContext, List<T> mDatas){
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    public List<T> getDatas() {
        return mDatas;
    }

    public void syncDatas(List<T> datas){
        if(mDatas != null) {
            if(datas != null && datas.size() > 0) {
                this.mDatas.clear();
                this.mDatas.addAll(datas);
                notifyDataSetChanged();
            }
        }else{
            this.mDatas = datas;
        }
    }

    public void setOnItemClickListener(OnItemClickListener<T> listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    //  删除指定的Item
    public void removeData(int position)
    {
        if(mDatas.size() > position) {
            mDatas.remove(position);
            //  通知RecyclerView控件某个Item已经被删除
            notifyItemRemoved(position);
            notifyItemRangeChanged(0,mDatas.size());
        }
    }

    //  在指定位置变更一个新的Item
    public void setItem(T model,int positionToSet)
    {
        mDatas.set(positionToSet,model);
        //  通知RecyclerView控件插入了某个Item
        notifyItemChanged(positionToSet);
        notifyItemRangeChanged(0,mDatas.size());
    }

    //  在指定位置添加一个新的Item
    public void addItem(T model,int positionToAdd)
    {
        mDatas.add(model);
        //  通知RecyclerView控件插入了某个Item
        notifyItemInserted(positionToAdd);
        notifyItemRangeChanged(0,mDatas.size());
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(), parent, false);
        return new BaseRecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final T data = getItemData(position);

        BaseRecyclerViewHolder viewHolder = (BaseRecyclerViewHolder) holder;

        onBind(viewHolder, position, data);

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(v, position, data);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        int count = 0;
        if (mDatas.size() > 0) {
            count = mDatas.size();
        }
        return count;
    }

    public T getItemData(int position){
        return mDatas.get(position);
    }

    protected abstract int getLayoutId();
    protected abstract void onBind(BaseRecyclerViewHolder holder, int position, T data);

    public class BaseRecyclerViewHolder extends RecyclerView.ViewHolder{
        private SparseArray<View> views;
        public BaseRecyclerViewHolder(View itemView) {
            super(itemView);
            views = new SparseArray<View>();
        }

        private <T extends View> T converToViewFromId(int resId) {
            View view = views.get(resId);
            if(view == null){
                view = itemView.findViewById(resId);
            }
            views.put(resId,view);
            return (T)view;
        }

        public View getView(int id) {
            return converToViewFromId(id);
        }

        public TextView getTextView(int id) {
            return converToViewFromId(id);
        }

        public ImageView getImageView(int id) {
            return converToViewFromId(id);
        }


        public BaseRecyclerViewHolder setText(int id, int charSequence) {
            getTextView(id).setText(charSequence);
            return this;
        }

        public BaseRecyclerViewHolder setText(int id, CharSequence charSequence) {
            getTextView(id).setText(charSequence);
            return this;
        }

        public BaseRecyclerViewHolder setTextColor(int id, int color) {
            getTextView(id).setTextColor(color);
            return this;
        }

        public BaseRecyclerViewHolder setImageResource(int viewId, int imageResId) {
            getImageView(viewId).setImageResource(imageResId);
            return this;
        }

        public void setBackgroundColor(int id, int color) {
            converToViewFromId(id).setBackgroundColor(color);
        }
    }
}
